<?php
	class Action extends LiladsAppModel {
		var $name = 'Action';
		var $useTable = 'lilads_actions';
 
		function getStats(&$params, $len=10) {
			if (isset($params['start']) && !preg_match('%^(\d{4})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$%', $params['start'])) {
				unset($params['start']);
			}
			if (isset($params['end']) && !preg_match('%^(\d{4})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$%', $params['end'])) {
				unset($params['end']);
			}

			$conditions = array();
			if (!empty($params['campaign_id'])) $conditions['campaign_id'] = (int)$params['campaign_id'];
			if (!empty($params['banner_id'])) $conditions['banner_id'] = (int)$params['banner_id'];
			if (!empty($params['start'])) $conditions[] = array('date_hour >=' => $params['start']);
			if (!empty($params['end'])) $conditions[] = array('date_hour <=' => $params['end']);
			// lil' hack
			$conditions[] = '1=1 GROUP BY LEFT(date_hour, '.$len.')';
			
			return $this->find('all', array(
				'fields'     => array('SUM(clicks) AS clicks', 'SUM(views) AS views', 'LEFT(date_hour, '.$len.') AS date_hour'), 
				'conditions' => $conditions, 
				'order'      => 'date_hour'));
		}
	}
?>